package zmee.bid.formplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.botp.plugin.AbstractConvertPlugIn;
import kd.bos.entity.botp.plugin.args.AfterConvertEventArgs;

public class PmctOutcontractConvert extends AbstractConvertPlugIn {

    @Override
    public void afterConvert(AfterConvertEventArgs e) {
        ExtendedDataEntity[] entities = e.getTargetExtDataEntitySet().FindByEntityKey(this.getTgtMainType().toString());
        for (ExtendedDataEntity entity:entities) {
            DynamicObject dataEntity = entity.getDataEntity();
            DynamicObjectCollection addmodelentry = dataEntity.getDynamicObjectCollection("listmodelentry");
            if(addmodelentry.size()>1){
                //第一条清单
                DynamicObject cardentry = addmodelentry.get(0);
                //子单据体
                DynamicObjectCollection ent = cardentry.getDynamicObjectCollection("sublistentry");
                for (int i = 1; i < addmodelentry.size(); i++) {
                    //子单据体
                    DynamicObjectCollection collection = addmodelentry.get(i).getDynamicObjectCollection("sublistentry");
                    for (DynamicObject col:collection) {
                        ent.add(col);
                    }
                    //清空子单据体中的数据
                    collection.clear();
                }
                //只保留一张清单模板，删除多余的清单模板
                while (true){
                    if(addmodelentry.size()==1){
                        break;
                    }
                    addmodelentry.remove(1);
                }
            }
        }
    }
}
